package Function;

import FCRendering.FCRMaster;
import VWorldGameStudio.*;

import java.util.ArrayList;

import static FCRendering.FCRMaster.*;

/**
 * 物品的破防，减少血的都要在这里弄，方便统一
 */
public class FCItemForTankHelper {

    public static FCTank findTankHasBag() {
        FCTank tank = tankLib.getTankOfID(player1.getPlayertank1(), player1.getPlayertankHostId());
        if (tank != null && tank.hasEmptyBag()) {
            return tank;
        }

        //这里帮了别人的坦克补了状态
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            if (!link.curPlayerHostUID.equals(gameSave.HOST_UID)) {
                continue;
            }
            FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (p != null) {
                tank = tankLib.getTankOfID(p.getPlayertank1(), p.getPlayertankHostId());
                if (tank != null && tank.hasEmptyBag()) {
                    return tank;
                }
            }
        }
        //还有牵引的
        for (FCPropulsion fc : fcv.getPropulsions()) {
            if (fc.getPropulsionHostId().equals(gameSave.HOST_UID) && fc.getPropulsionType().equals("tank")) {
                if (((FCTank) fc).hasEmptyBag()) {
                    return (FCTank) fc;
                }
            }
        }
        return null;
    }

    /**
     * 进行了修理
     */
    public static void fixItem(FCItem item) {
        item.setCurrentDefense(item.getDefenseAdd() + item.getDefenseUpgradeAdd());
    }

    /**
     * 满仓弹药
     *
     * @param item
     */
    public static void fullDanyao(FCItem item) {
        item.setCurrentCapacityVal((int) item.getCapacity());
    }

    /**
     * 用了一发弹药
     *
     * @param item
     */
    public static void subDanyao(FCItem item) {
        if (item.getCapacity() > 0) {//如果是0那就是无限弹的，
            item.setCurrentCapacityVal(Math.max(item.getCurrentCapacityVal() - 1, 0));
        }
    }

    /**
     * 获取弹药要多少发可以补满
     *
     * @param item
     * @return
     */
    public static int getFullDanyaoNeed(FCItem item) {
        return (int) (item.getCapacity() - item.getCurrentCapacityVal());
    }

    /**
     * 物品是否是损还是坏了，物品血量小于3份1就是损，等于0就是坏了
     *
     * @param item
     * @return == 0 就是正常  == -1就是损 == -2就是坏掉
     */
    public static int getItemState(FCItem item) {
        if (item.getCurrentDefense() == 0)
            return -2;
        if (item.getCurrentDefense() < item.getDefenseAdd() / 3) {
            return -1;
        }
        return 0;
    }


    /**
     * 底盘增加特殊炮弹的弹药  一个弹仓定最大8发特殊炮弹
     *
     * @param itemId
     * @param count
     * @return
     */
    public static boolean addShellCount(FCItem dipan, int num, int count) {
        if (dipan.getShellCount()[num] <= 16) {
            dipan.getShellCount()[num] += count;
            dipan.getShellCount()[num] = Math.min(16, dipan.getShellCount()[num]);
            return true;
        }
        return false;
    }

    public static boolean subShellIndex(FCItem dipan, int num, int index) {
        dipan.getShellCount()[index] -= num;
        dipan.getShellCount()[index] = Math.max(0, dipan.getShellCount()[index]);
        if (dipan.getShellCount()[index] == 0) {
            dipan.getShellCaseType()[index] = "null";//打完了
        }
        return true;
    }

    /**
     * 计算底盘的所有特殊弹药重量
     *
     * @return
     */
    public static double getAllShellWeight(FCItem item) {
        double allWeight = 0;
        for (int i = 0; i < item.getShellCaseNum(); i++) {
            FCItem item1 = FCRMaster.itemLib.getItemOfID(item.getShellCaseType()[i]);
            if (item1 != null) {
                allWeight += (item1.getWeight() * item.getShellCount()[i]);
            }
        }
        return allWeight;
    }

    /**
     * 获取炮弹的名称和弹数
     *
     * @return
     */
    public static ArrayList<String> getShellList(FCItem dipanItem) {
        ArrayList<String> list = new ArrayList<>(0);
        for (int i = 0; i < dipanItem.getShellCaseNum(); i++) {
            FCItem item1 = FCRMaster.itemLib.getItemOfID(dipanItem.getShellCaseType()[i]);
            if (item1 != null) {
                list.add(item1.getName() + "-" + dipanItem.getShellCount()[i] + "-" + i);
            }
        }
        return list;
    }
}
